<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2020/5/22
 * Time: 15:23
 */

namespace app\admin\controller;

use think\Db;
use think\Request;

class Icon extends Base
{
    /**
     * 首页展示
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax ()){
            $where['icon_level'] = 1;
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('icon')->where($where)->count();// 获取总条数
            $lists = Db::name('icon')
                ->where($where)
                ->field('icon_id, icon_name, icon_thumb, icon_sort, icon_status')
                ->page($Nowpage,$limits)
                ->order('icon_sort asc')
                ->select();
            
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    
    /**
     * 详情
     * @return mixed
     */
    public function iconDetail(){
        $icon_id = input('icon_id');
        $icon = Db::table('up_icon')
            ->where('icon_id', $icon_id)
            ->field('icon_name, icon_content, icon_thumb, icon_sort, icon_status')
            ->find();
        return $this->fetch('icon/icondetail', ['icon' => $icon]);
    }
    
    /**
     * 编辑图标
     * @return mixed|\think\response\Json
     */
    public function iconEdit(){
        $icon_id = input('icon_id');
        if(Request::instance()->isPost()){
            $icon_name = trim(input('icon_name'), ' ');
            $icon_content = trim(input('icon_content'), ' ');
            $icon_thumb = input('icon_thumb');
            $update = [
                'icon_name' => $icon_name,
                'icon_content' => $icon_content,
                'icon_update_time' => date('Y-m-d H:i:s')
            ];
            if(!empty($icon_thumb)){
                $update['icon_thumb'] = $icon_thumb;
            }
            $res = Db::table('up_icon')->where('icon_id', $icon_id)->update($update);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $icon = Db::table('up_icon')
            ->where('icon_id', $icon_id)
            ->field('icon_name, icon_content, icon_thumb')
            ->find();
        $this->assign([
            'icon_id' => $icon_id,
            'icon' => $icon
        ]);
        return $this->fetch('icon/iconedit');
    }
    
    /**
     * 编辑排序
     * @return \think\response\Json
     */
    public function updateSort(){
        $icon_id = input('id');
        $update = [
            'icon_sort'=>input('value'),
            'icon_update_time' => date('Y-m-d H:i:s')
        ];
        $res = Db::name('icon')->where('icon_id',$icon_id)->update($update);
        if($res){
            return json(['code' => 200, 'msg' => '修改成功']);
        }else{
            return json(['code' => 100, 'msg' =>' 修改失败']);
        }
    }
    
    /**
     * 隐藏
     * @return \think\response\Json
     */
    public function iconDown(){
        $icon_id = input('id');
        $res = Db::name('icon')->where('icon_id',$icon_id)->setField('icon_status', 2);
        if($res){
            return json(['code' => 200, 'msg' => '隐藏成功']);
        }else{
            return json(['code' => 100, 'msg' =>' 隐藏失败']);
        }
    }
    
    /**
     * 展示
     * @return \think\response\Json
     */
    public function iconOn(){
        $icon_id = input('id');
        $res = Db::name('icon')->where('icon_id',$icon_id)->setField('icon_status', 1);
        if($res){
            return json(['code' => 200, 'msg' => '展示成功']);
        }else{
            return json(['code' => 100, 'msg' =>' 展示失败']);
        }
    }
    
    /**
     * 二级页面展示
     * @return mixed|\think\response\Json
     */
    public function secondIndex(){
        if(request()->isAjax ()){
            $where['icon_level'] = 2;
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('icon')->where($where)->count();// 获取总条数
            $lists = Db::name('icon')
                ->where($where)
                ->field('icon_id, icon_name, icon_thumb')
                ->page($Nowpage,$limits)
                ->order('icon_pid desc')
                ->select();
        
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch('icon/secondindex');
    }
    
    /**
     * 二级页面内容详情
     * @return mixed
     */
    public function iconDetailS(){
        $icon_id = input('icon_id');
        $icon = Db::table('up_icon')
            ->where('icon_id', $icon_id)
            ->field('icon_id, icon_name, icon_content, icon_thumb')
            ->find();
        return $this->fetch('icon/icondetails', ['icon' => $icon]);
    }
    
    /**
     * 编辑二级页面内容
     * @return mixed|\think\response\Json
     */
    public function iconEditS(){
        $icon_id = input('icon_id');
        if(Request::instance()->isPost()){
            $icon_name = trim(input('icon_name'), ' ');
            $icon_content = trim(input('icon_content'), ' ');
            $icon_thumb = input('icon_thumb');
            $icon_link = trim(input('icon_link'), ' ');
            if($icon_id == 14 || $icon_id == 15){
                $icon_content = $icon_link;
            }
            $update = [
                'icon_name' => $icon_name,
                'icon_content' => $icon_content,
                'icon_update_time' => date('Y-m-d H:i:s')
            ];
            if(!empty($icon_thumb)){
                $update['icon_thumb'] = $icon_thumb;
            }
            $res = Db::table('up_icon')->where('icon_id', $icon_id)->update($update);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $icon = Db::table('up_icon')
            ->where('icon_id', $icon_id)
            ->field('icon_name, icon_content, icon_thumb')
            ->find();
        $this->assign([
            'icon_id' => $icon_id,
            'icon' => $icon
        ]);
        return $this->fetch('icon/iconedits');
    }
}